import api from '@/api';
import _ from 'lodash';

export default {
  namespace: 'order',

  state: {
    statusEnum: [],
    statusMap: {},
    currentOrder: {},
    orderAddress: {},
    valuedServices: [],
    optionServices: [],
    attachments: [],
    fileTypes: [],
    goods: [],
    orderFee: {},
    orderFeeNew: {},
    trackInfo: {},
    serviceList: [],
    workOrderType: [],
    workOrderPriority: [],
    workOrder: {
      list: [],
      pagination: {},
    },
    orderChildType: [],
    orderSource: [],
    abnormalOrders: {
      list: [],
      pagination: {},
    },
    abnormalOrderType: [],
    journalList: [],
    cancelStatus: null,
    receiptList: [],
    queryCondition: {},
    workOrderMessageCount: 0,
    abnormalMessageCount: 0,
    carrierInfo: null,
    insuranceInfo: {},
    waybillNo: null,
  },

  effects: {
    *fetchCarrierInfo({ payload }, { call, put }) {
      const response = yield call(api.order.carrierInfo, payload);
      yield put({
        type: 'saveCarrierInfo',
        payload: response.data,
      });
    },
    *fetchInsurance({ payload }, { call, put }) {
      const response = yield call(api.order.insurance, payload);
      yield put({
        type: 'saveInsurance',
        payload: response.data,
      });
    },
    *queryAllUsers({ payload }, { call }) {
      return yield call(api.role.queryUsers, payload);
    },
    *checkOrder({ payload }, { call }) {
      return yield call(api.order.checkOrder, payload);
    },
    *fetchLogisticsByWaybillNos({ payload }, { call }) {
      const response = yield call(api.order.queryLogisticsByWaybillNos, payload);
      return response;
    },
    *fetchGroupCode(_, { call }) {
      const response = yield call(api.order.queryGroupCode);
      return response;
    },
    *updateLogistics({ payload }, { call }) {
      const response = yield call(api.order.updateLogistics, payload);
      return response;
    },
    *replyAbnormalOrder({ payload }, { call }) {
      const response = yield call(api.order.replyAbnormalOrder, payload);
      return response;
    },
    *updateAbnormalOrder({ payload }, { call }) {
      const response = yield call(api.order.updateAbnormalOrder, payload);
      return response;
    },
    *fetchAbnormalById({ payload }, { call }) {
      const response = yield call(api.order.queryAbnormalById, payload);
      return response;
    },
    *fetchAbnormalOrderType(_, { call }) {
      const response = yield call(api.order.queryAbnormalOrderType);
      return response;
    },
    *fetchAbnormalOrders({ payload }, { call, put }) {
      let orderType = {
        1: 'queryToDoAbnormalOrders',
        2: 'queryPassAbnormalOrders',
        3: 'queryAbnormalOrders',
      }[payload.type];
      const response = yield call(api.order[orderType], payload.data);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveAbnormalOrders',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
    },
    *fetchAbnormalOrdersCount(_, { call, put }) {
      const response = yield call(api.order.queryTodoAbnormalOrdersCount);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveAbnormalMessageCount',
          payload: response.data
        })
      }
      return response;
    },
    *fetchWorkOrderByOrder({ payload }, { call }) {
      const response = yield call(api.order.queryWorkOrderByOrder, payload);
      return response;
    },
    *queryWorkOrderMessageCount({ payload }, { call, put }) {
      const response = yield call(api.order.queryWorkOrderMessageCount, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveWorkOrderMessageCount',
          payload: response.data.todoCount
        })
      }
      return response;
    },
    *reply({ payload }, { call }) {
      const response = yield call(api.order.reply, payload);
      return response;
    },
    *queryCreatorByOrder({ payload }, { call }) {
      const response = yield call(api.order.queryCreatorByOrder, payload);
      return response;
    },
    *queryCreatorByHuiOrder({ payload }, { call }) {
      const response = yield call(api.order.queryCreatorByHuiOrder, payload);
      return response;
    },
    *fetchOrderSource(_, { call, put }) {
      const response = yield call(api.order.queryOrderSource);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrderSource',
          payload: response.data,
        });
      }
    },
    *fetchOrderChildType({ payload }, { call, put }) {
      const response = yield call(api.order.queryOrderChildType, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrderChildType',
          payload: response.data,
        });
      }
    },
    *fetchWorkOrders({ payload }, { call, put }) {
      let orderType = {
        2: 'queryCreateWorkOrders',
        1: 'queryToDoWorkOrders',
        3: 'queryAllWorkOrders',
        4: 'queryHandleWorkOrders',
      }[payload.type];
      const response = yield call(api.order[orderType], payload.data);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveWorkOrders',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
    },
    *fetchWorkOrderById({ payload }, { call }) {
      const response = yield call(api.order.queryWorkOrderById, payload);
      return response;
    },
    *fetchWorkOrderPriority(_, { call, put }) {
      const response = yield call(api.order.queryWorkOrderPriority);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveWorkOrderPriority',
          payload: response.data,
        });
      }
    },
    *fetchWorkOrderType(_, { call, put }) {
      const response = yield call(api.order.queryWorkOrderType);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveWorkOrderType',
          payload: response.data,
        });
      }
    },
    *createWorkOrder({ payload }, { call }) {
      const response = yield call(api.order.createWorkOrder, payload);
      return response;
    },
    *queryOrderFlightInfo({ payload }, { call }) {
      const response = yield call(api.order.queryOrderFlightInfo, payload);
      return response;
    },
    *resetOrderModal({ payload }, { call }) {
      const response = yield call(api.order.resetOrderModal, payload);
      return response;
    },
    *updateWorkOrder({ payload }, { call }) {
      const response = yield call(api.order.updateWorkOrder, payload);
      return response;
    },
    *fetchStatusEnum(test, { call, put }) {
      const response = yield call(api.order.queryStatusEnum);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveStatusEnum',
          payload: response.data,
        });
      }
    },
    *fetchCurrent({ payload }, { call, put, select }) {
      let currentOrder = yield select(state => state.order.currentOrder);
      if (_.isEmpty(currentOrder)) {
        const response = yield call(api.order.queryByWaybillNo, payload);
        if (response.errorCode === 0) {
          yield put({
            type: 'saveCurrent',
            payload: response.data,
          });
        }
        return response;
      } else {
        return {
          errorCode: 0,
          data: currentOrder,
        };
      }
    },
    *fetchStatusMap({ payload }, { call, put }) {
      const response = yield call(api.order.queryStatusDetail, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveStatusMap',
          payload: response.data,
        });
      }
    },
    *fetchOrderAddress({ payload }, { call, put }) {
      const response = yield call(api.order.queryAddress, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrderAddress',
          payload: response.data,
        });
      }
      return response;
    },
    *fetchValuedServices({ payload }, { call, put }) {
      const response = yield call(api.order.queryValuedServices, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveValuedServices',
          payload: response.data,
        });
      }
    },
    *fetchOptionServices({ payload }, { call, put }) {
      const response = yield call(api.order.queryOptionServices, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOptionServices',
          payload: response.data,
        });
      }
    },
    *fetchGoods({ payload }, { call, put }) {
      const response = yield call(api.order.queryGoods, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveGoods',
          payload: response.data,
        });
      }
    },
    *fetchFileTypes(_, { call, put }) {
      const response = yield call(api.order.queryFileTypes);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveFileTypes',
          payload: response.data,
        });
      }
    },
    *fetchAttachments({ payload }, { call, put }) {
      const response = yield call(api.order.queryAttachments, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveAttachments',
          payload: response.data,
        });
      }
    },
    *fetchOrderFee({ payload }, { call, put }) {
      const response = yield call(api.order.queryOrderFee, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrderFee',
          payload: response.data,
        });
      }
    },
    *fetchOrderFeeNew({ payload }, { call, put }) {
      const response = yield call(api.order.queryOrderFeeNew, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveOrderFeeNew',
          payload: response.data,
        });
      }
    },
    *fetchTrackInfo({ payload }, { call, put }) {
      const response = yield call(api.order.queryTrackInfo, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveTrackInfo',
          payload: response.data,
        });
      }
    },
    *fetchServiceList({ payload }, { call, put }) {
      const response = yield call(api.order.queryServiceList, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveServiceList',
          payload: response.data,
        });
      }
    },
    *fetchOrderList({ payload }, { call, put }) {
      return yield call(api.order.queryOrderList, payload);
    },
    *orderModify({ payload }, { call }) {
      return yield call(api.order.orderModify, payload);
    },
    *orderCancel({ payload }, { call }) {
      return yield call(api.order.orderCancel, payload);
    },
    *orderCancelStatus({ payload }, { call, put }) {
      const response = yield call(api.order.orderCancelStatus, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCancelStatus',
          payload: response.data,
        });
      }
    },
    *orderCompensation({ payload }, { call }) {
      const response = yield call(api.order.retry, payload);
      return response;
    },
    *fetchPaymentChildFees({ payload }, { call }) {
      const url = api.order.paymentChilds;
      const response = yield call(url, payload);
      return response;
    },
    *fetchReceiptChildFees({ payload }, { call }) {
      const url = api.order.receiptChilds;
      const response = yield call(url, payload);
      return response;
    },
    *fetchJournalList({ payload }, { call, put }) {
      const response = yield call(api.order.journalList, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveJournalList',
          payload: response.data,
        });
      }
    },
    *fetchReceiptList({ payload }, { call, put }) {
      const response = yield call(api.order.queryReceiptList, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveReceiptList',
          payload: response.data,
        });
      }
    },
    *orderCancelNew({ payload }, { call }) {
      return yield call(api.order.orderCancelNew, payload);
    },
    *checkPrescription({ payload }, { call }) {
      return yield call(api.order.checkPrescription, payload);
    },
    *stopOrder({ payload }, { call }) {
      return yield call(api.order.stopOrder, payload);
    },
    *changeFlight({ payload }, { call }) {
      return yield call(api.order.changeFlight, payload);
    },
  },

  reducers: {
    saveWorkOrderMessageCount(state, { payload }) {
      return {
        ...state,
        workOrderMessageCount: payload
      }
    },
    saveAbnormalMessageCount(state, { payload }) {
      return {
        ...state,
        abnormalMessageCount: payload
      }
    },
    saveAbnormalOrders(state, { payload }) {
      return {
        ...state,
        abnormalOrders: payload,
      };
    },
    saveOrderSource(state, { payload }) {
      return {
        ...state,
        orderSource: payload,
      };
    },
    saveOrderChildType(state, { payload }) {
      return {
        ...state,
        orderChildType: payload,
      };
    },
    saveWorkOrders(state, { payload }) {
      return {
        ...state,
        workOrder: payload,
      };
    },
    saveWorkOrderPriority(state, { payload }) {
      return {
        ...state,
        workOrderPriority: payload,
      };
    },
    saveWorkOrderType(state, { payload }) {
      return {
        ...state,
        workOrderType: payload,
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentOrder: payload,
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        statusMap: {},
        currentOrder: {},
        orderAddress: {},
        valuedServices: [],
        optionServices: [],
        attachments: [],
        fileTypes: [],
        goods: [],
        orderFee: {},
        trackInfo: {},
        serviceList: [],
        serviceNode: {},
        cancelStatus: null,
      };
    },
    saveStatusEnum(state, { payload }) {
      return {
        ...state,
        statusEnum: payload,
      };
    },
    saveStatusMap(state, { payload }) {
      return {
        ...state,
        statusMap: payload,
      };
    },
    saveOrderAddress(state, { payload }) {
      return {
        ...state,
        orderAddress: payload,
      };
    },
    saveValuedServices(state, { payload }) {
      return {
        ...state,
        valuedServices: payload,
      };
    },
    saveOptionServices(state, { payload }) {
      return {
        ...state,
        optionServices: payload,
      };
    },
    saveGoods(state, { payload }) {
      return {
        ...state,
        goods: payload,
      };
    },
    saveFileTypes(state, { payload }) {
      return {
        ...state,
        fileTypes: payload,
      };
    },
    saveAttachments(state, { payload }) {
      return {
        ...state,
        attachments: payload,
      };
    },
    saveOrderFee(state, { payload }) {
      return {
        ...state,
        orderFee: payload,
      };
    },
    saveOrderFeeNew(state, { payload }) {
      return {
        ...state,
        orderFeeNew: payload,
      };
    },
    saveTrackInfo(state, { payload }) {
      return {
        ...state,
        trackInfo: payload,
      };
    },
    saveServiceList(state, { payload }) {
      return {
        ...state,
        serviceList: payload,
      };
    },
    saveCancelStatus(state, { payload }) {
      return {
        ...state,
        cancelStatus: payload,
      };
    },
    saveJournalList(state, { payload }) {
      return {
        ...state,
        journalList: payload,
      };
    },
    saveReceiptList(state, { payload }) {
      return {
        ...state,
        receiptList: payload,
      };
    },
    saveQueryCondition(state, { payload }) {
      return {
        ...state,
        queryCondition: payload,
      };
    },
    deleteQueryCondition(state) {
      return {
        ...state,
        queryCondition: {},
      };
    },
    saveCarrierInfo(state, { payload }) {
      return {
        ...state,
        carrierInfo: payload
      };
    },
    saveInsurance(state, { payload }) {
      return {
        ...state,
        insuranceInfo: payload
      };
    },
    saveWaybillNo(state, { payload }) {
      return {
        ...state,
        waybillNo: payload
      }
    },
    clearWaybillNo(state) {
      return {
        ...state,
        waybillNo: null
      }
    },
  },
};
